using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Build.Evaluation;

namespace ebc.viz.modelbuilder
{
    public class Collect_board_definitions_from_project : ebc.viz.contracts.ICollect_board_definitions_from_project
    {
        public void Process(string[] projFilenames)
        {
            Trace.TraceInformation("Collect board definitions from {0}", string.Join(",", projFilenames.Select(Path.GetFileName)));

            var filenames = new List<string>();
            projFilenames.ToList().ForEach(f => filenames.AddRange(Extract_board_filenames(f)));
            this.Filenames(filenames.ToArray());
        }


        internal static IEnumerable<string> Extract_board_filenames(string projFilename)
        {
            var prj = new Project(projFilename);
            var ebcXmlfilenames = prj.Items
                                     .Where(item => item.ItemType == "Content")
                                     .Select(item => item.EvaluatedInclude)
                                     .Where(filename => filename.ToLower().EndsWith(".ebc.xml"));
            return ebcXmlfilenames;
        }


        public event Action<string[]> Filenames;
    }
}